package com.boarsoft.boar.soagov.nexus;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.entity.SoaNexus;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("soaNexusBiz")
public class NexusBizImpl extends SimpleBizImpl implements NexusBiz {
	@Override
	@Transactional(readOnly = true)
	public PagedResult<SoaNexus> list(String objType0, String objId0, String objType1, String objId1, String key,
			String orderBy, int pageNo, int pageSize) {
		StringBuilder sb = new StringBuilder();
		if (Util.strIsNotEmpty(objType0)) {
			sb.append(" and n.objType0='").append(objType0).append("'");
		}
		if (Util.strIsNotEmpty(objId0)) {
			sb.append(" and n.objId0='").append(objId0).append("'");
		}
		if (Util.strIsNotEmpty(objId1)) {
			sb.append(" and n.objType1='").append(objType1).append("'");
		}
		if (Util.strIsNotEmpty(objId1)) {
			sb.append(" and n.objId1='").append(objId1).append("'");
		}
		if (Util.strIsNotEmpty(key)) {
			sb.append(" and (n.objCode0 like '%").append(key).append("%' or n.objCode1").append(" like '%").append(key)
					.append("%' or n.objName0 like '%").append(key).append("%' or n.objName1").append(" like '%").append(key)
					.append("%')");
		}
		int total = dao.getTotal(SoaNexus.class, sb.toString());
		//
		if (Util.strIsNotEmpty(orderBy)) {
			// String ob = Util.array2str(("e." + orderBy).replaceAll(",",
			// ",").split(","), ",");
			sb.append(" order by ").append(orderBy);
		} else {
			sb.append(" order by n.objId0, n.objId1");
		}
		List<SoaNexus> lt = dao.list("select n".concat(sb.toString()), pageNo, pageSize);
		return new PagedResult<SoaNexus>(total, lt, pageNo, pageSize);
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> listByMe(String objId0) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and objId0='").append(objId0).append("'");
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional(readOnly = true)
	public List<SoaNexus> listOnMe(String objId1) {
		StringBuilder sb = new StringBuilder();
		sb.append(" and objId1='").append(objId1).append("'");
		return dao.list(SoaNexus.class, sb.toString());
	}

	@Override
	@Transactional
	public void delete(String id) {
		dao.delete(SoaNexus.class, id);
	}

	@Override
	@Transactional
	public ReplyInfo<Object> save(SoaNexus p) {
		if (Util.strIsNotEmpty(p.getId())) {
			dao.merge(p);
		} else {
			StringBuilder sb = new StringBuilder();
			sb.append("select id from ").append(SoaNexus.class.getName());
			sb.append(" where objId0='").append(p.getObjId0());
			sb.append("' and objId1='").append(p.getObjId1());
			sb.append("' and way='").append(p.getWay()).append("'");
			sb.append("' and async=").append(p.getAsync());
			String id = (String) dao.getValue(sb.toString());
			if (Util.strIsNotEmpty(id)) {
				p.setId(id);
				dao.merge(p);
			} else {
				dao.save(p);
			}
		}
		return new ReplyInfo<Object>(true, p.getId());
	}

	@Override
	@Transactional(readOnly = true)
	public SoaNexus get(String id) {
		return (SoaNexus) dao.get(SoaNexus.class, id);
	}

	@Override
	@Transactional
	public void save(String id, List<SoaNexus> nLt) {
		// 假设 A引用B，B引用C，C引用D，先保存 A->B 的关系
		StringBuilder sb = new StringBuilder();
		sb.append("delete from ").append(SoaNexus.class.getName());
		sb.append(" where objId0='").append(id).append("'");
		dao.executeUpdate(sb.toString());
		dao.batchSave(nLt, 50);
	}
}